package oa.hleast.xswl.common.util;

import cn.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * 参数签名工具类
 */
@Slf4j
public class SignUtils {
    /**
     *
     * @Description: 签名：请求参数排序并后面补充key值，最后进行MD5加密，返回大写结果
     * @author: tianpengw
     * @param params 参数内容
     * @param key key值
     * @return
     */
    public static String signatures(Map<String, Object> params, String path, String key, String secret, String timestamp){
        String signatures = "";
        try {
            List<String> paramsStr = new ArrayList<String>();
            for (String key1 : params.keySet()) {
                if(null != key1 && !"".equals(key1)){
                    paramsStr.add(key1);
                }
            }
            Collections.sort(paramsStr);
            StringBuilder sbff = new StringBuilder();
            //加上key值
            sbff.append("key="+key);
            sbff.append("&secret="+secret);
            sbff.append("&timestamp="+timestamp);
            sbff.append("&path="+path);
            for (String kk : paramsStr) {
                String value = params.get(kk).toString();
                    sbff.append("&" + kk + "=" + value);
            }
            System.out.println(sbff.toString());
            signatures = SecureUtil.md5(sbff.toString()).toLowerCase();
        }catch(Exception e) {
            e.printStackTrace();
        }
        return signatures;
    }

    public static void main(String[] args) {
        String path="GET_/wl-sms/v1/place-type/findShowPlaceInfo";
        Map<String, Object> params = new HashMap<>();
        params.put("placeTypeId", 124);
        params.put("name", "广");
        System.out.println(signatures(params, path, "wl-app", "123456", "20210712161401"));
    }
}
